package com.batis.source.io;

import java.io.InputStream;

/**
 * @author isam2016
 * @create 2021-08-26 10:40 AM
 */
public class MYClassLoaderWrapper {
    ClassLoader defaultClassLoader;

    MYClassLoaderWrapper(){

    }

    /**
     * 通过路径访问 资源
     * @param resource
     * @return
     */
    public InputStream getResourceAsStream(String resource){
        return getResourceAsStream(resource, new ClassLoader[]{
                defaultClassLoader,
                Thread.currentThread().getContextClassLoader(),
                getClass().getClassLoader(),
                ClassLoader.getSystemClassLoader()
        });
    }

    /**
     * 尝试从一组加载器中获取资源
     */
    InputStream getResourceAsStream(String resource,  ClassLoader[] classLoader) {
        for (ClassLoader cl : classLoader) {
            if (null != cl) {
                InputStream returnValue = cl.getResourceAsStream(resource);
                if (null != returnValue) return returnValue;
            }
        }
        return null;
    }

}
